package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import seekting.demo2017.AppEnv;
import seekting.demo2017.retrofit.HttpGsonConverterFactory;
import seekting.demo2017.retrofit.News;
import seekting.demo2017.retrofit.NewsBean;

/**
 * Created by seekting on 17-5-18.
 */

public class RetrofitActivity extends Activity {

    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = RetrofitActivity.class.getSimpleName();

    public static final String HOST = "https://c.m.163.com/nc/article/headline/T1348647909107/ ";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate.");
        }
//        Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
//        Retrofit r = retrofitBuilder.baseUrl(HOST).build();
//        r.callAdapter()
        long es = SystemClock.uptimeMillis();
        Log.d("seekting", "RetrofitActivity.onCreate()es=" + es);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HOST)
                .addConverterFactory(HttpGsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        News news = retrofit.create(News.class);
        Observable<List<NewsBean>> observable = news.getNews();
        observable = observable.subscribeOn(Schedulers.io());
        observable = observable.observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new Subscriber<List<NewsBean>>() {
            @Override
            public void onCompleted() {
                Log.d("seekting", "RetrofitActivity.onCompleted()");

            }

            @Override
            public void onError(Throwable e) {
                Log.d("seekting", "RetrofitActivity.onError()", e);

            }

            @Override
            public void onNext(List<NewsBean> newses) {
                Log.d("seekting", "RetrofitActivity.onNext()" + newses);

            }
        });


    }


}
